package com.jt.blog.service;

import com.jt.blog.common.model.TreeNode;
import com.jt.blog.dto.CommentDto;
import com.jt.blog.mapper.CommentMapper;
import com.jt.blog.model.Comment;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiangtao
 * @create 2016-09-21 14
 **/
@Service
public class CommentReadServiceImpl extends AbstractBaseService<Comment> implements CommentReadService {

    @Override
    public TreeNode<CommentDto> get(Long blogId, Integer sort) {
        CommentMapper commentMapper = (CommentMapper) mapper;
        List<CommentDto> comments = commentMapper.getByBlogId(blogId);
        TreeNode<CommentDto> root = new TreeNode<>();
        cycleHandle(root,comments);
        return root;
    }

    private void cycleHandle(TreeNode<CommentDto> father,List<CommentDto> comments){
        CommentDto fc = father.getNode();
        for(CommentDto comment : comments){
           if((fc==null&&comment.getPid()==null)||
                   (fc!=null&&fc.getId()==comment.getPid())){
               TreeNode<CommentDto> node = new TreeNode<>(comment);
               father.addChild(node);
               cycleHandle(node,comments);
           }
        }
    }
}
